﻿using Microsoft.CSharp;
using Microsoft.JScript;
using Microsoft.JScript.Vsa;
using System;
using System.CodeDom.Compiler;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;

namespace tmAsk.Data
{

    public class MakeHtml
    {
        private MakehtmlSQL get = new MakehtmlSQL();

        public int add(string sql, SqlParameter[] cmdparm)
        {
            return this.get.Add(sql, cmdparm);
        }

        public void createfile(string path)
        {
            if (!Filetemplate.createfile(path))
            {
                HttpContext.Current.Response.Write("Error:003 创建文件失败");
            }
        }

        private string datahtml(string value)
        {
            string pattern = Refun.Return_fun("e");
            int index = 0;
            int num3 = 0;
            int num4 = 0;
            try
            {
                if (!Regex.IsMatch(value, pattern))
                {
                    return value;
                }
                foreach (Match match in Regex.Matches(value, pattern))
                {
                    index = match.Value.IndexOf("$");
                    num3 = match.Value.IndexOf("@");
                    num4 = match.Value.IndexOf("}");
                    value = value.Replace(match.Value, this.repfilename(match.Value.Substring(3, index - 3), match.Value.Substring(index + 1, (num3 - index) - 1), match.Value.Substring(num3 + 1, (num4 - num3) - 1)));
                }
            }
            catch (Exception exception)
            {
                File.AppendAllText(HttpContext.Current.Server.MapPath(string.Format(@"\log\{0}.txt", DateTime.Now.Ticks.ToString())), "模板标签：数据库字段 错误 【 " + exception.Message + " 】 文件源：" + HttpContext.Current.Request.Path + "\t\r\n " + value);
                HttpContext.Current.Response.Write("模板标签 数据库字段 有错误信息，请查看日志</a>");
            }
            return value;
        }

        public int DeleteFile(string path)
        {
            return Fileuploads.DeleteFile(path);
        }

        private object EvalExpress(string sExpression)
        {
            return Evaluator.Eval(sExpression);
            // VsaEngine engine = VsaEngine.CreateEngine();
            // return Eval.JScriptEvaluate(sExpression, engine);
        }

        public class Evaluator
        {
            /// <summary>
            /// 计算结果,如果表达式出错则抛出异常
            /// </summary>
            /// <param name="statement">表达式,如"1+2+3+4"</param>
            /// <returns>结果</returns>
            public static object Eval(string statement)
            {
                return _evaluatorType.InvokeMember(
                            "Eval",
                            BindingFlags.InvokeMethod,
                            null,
                            _evaluator,
                            new object[] { statement }
                         );
            }

            static Evaluator()
            {
                //构造JScript的编译驱动代码
                CodeDomProvider provider = CodeDomProvider.CreateProvider("JScript");

                CompilerParameters parameters;
                parameters = new CompilerParameters();
                parameters.GenerateInMemory = true;

                CompilerResults results;
                results = provider.CompileAssemblyFromSource(parameters, _jscriptSource);

                Assembly assembly = results.CompiledAssembly;
                _evaluatorType = assembly.GetType("Evaluator");

                _evaluator = Activator.CreateInstance(_evaluatorType);
            }

            private static object _evaluator = null;
            private static Type _evaluatorType = null;

            /// <summary>
            /// JScript代码
            /// </summary>
            private static readonly string _jscriptSource =
                @"class Evaluator
              {
                  public function Eval(expr : String) : String 
                  { 
                     return eval(expr); 
                  }
              }";
        }

        public int ExecuteSql(string sql, SqlParameter[] cmdparm)
        {
            return this.get.ExecuteSql(sql, cmdparm);
        }

        private string forhtml(string html)
        {
            string pattern = Refun.Return_fun("b");
            string str2 = html;
            string id = "";
            int startIndex = 0;
            int num3 = 0;
            int index = 0;
            try
            {
                if (!Regex.IsMatch(html, pattern))
                {
                    return str2;
                }
                foreach (Match match in Regex.Matches(html, pattern))
                {
                    startIndex = match.Value.IndexOf(",") + 1;
                    num3 = match.Value.IndexOf("}") + 1;
                    index = match.Value.IndexOf("{/xunhuan}");
                    id = match.Value.Substring(num3, index - num3);
                    str2 = str2.Replace(match.Value, this.forhtml_return(System.Convert.ToInt32(match.Value.Substring(5, startIndex - 6)), System.Convert.ToInt32(match.Value.Substring(startIndex, (num3 - startIndex) - 1)), id));
                }
            }
            catch (Exception exception)
            {
                File.AppendAllText(HttpContext.Current.Server.MapPath(string.Format(@"\log\{0}.txt", DateTime.Now.Ticks.ToString())), "模板标签：xunhuan 错误 【 " + exception.Message + " 】 文件源：" + HttpContext.Current.Request.Path + "\t\r\n " + id);
                HttpContext.Current.Response.Write("模板标签 xunhuan 有错误信息，请查看日志</a>");
            }
            return str2;
        }

        private string forhtml_return(int a, int b, string id)
        {
            string str = "";
            for (int i = a; i < b; i++)
            {
                str = str + id.Replace("[xuhao]", i.ToString());
            }
            return str;
        }

        private string functionhtml(string html)
        {
            string pattern = Refun.Return_fun("a");
            string str2 = "";
            string str = "";
            string str4 = html;
            int index = 0;
            int num3 = 0;
            try
            {
                if (!Regex.IsMatch(html, pattern))
                {
                    return str4;
                }
                foreach (Match match in Regex.Matches(html, pattern))
                {
                    index = match.Value.IndexOf("@");
                    num3 = match.Value.IndexOf("}");
                    str2 = match.Value.Substring(8, index - 8);
                    str = match.Value.Substring(index + 1, (num3 - index) - 1);
                    str4 = str4.Replace(match.Value, this.functionhtml(str2, str));
                }
            }
            catch (Exception exception)
            {
                File.AppendAllText(HttpContext.Current.Server.MapPath(string.Format(@"\log\{0}.txt", DateTime.Now.Ticks.ToString())), "模板标签：function 错误 【 " + exception.Message + " 】 文件源：" + HttpContext.Current.Request.Path + "\t\r\n " + str4);
                HttpContext.Current.Response.Write("模板标签 function 有错误信息，请查看日志</a>");
            }
            return str4;
        }

        public virtual string functionhtml(string html, string str)
        {
            return "";
        }

        public string Get_html(string oldurl)
        {
            string html = "";
            html = this.readfiles(oldurl);
            html = this.includehtml(html);
            html = this.make_list(html);
            html = this.datahtml(html);
            html = this.ifhtml(html);
            html = this.forhtml(html);
            return this.functionhtml(html);
        }

        public string Get_html(string oldurl, string newurl)
        {
            string html = "";
            html = this.readfiles(oldurl);
            html = this.includehtml(html);
            html = this.html_makelist(html, newurl);
            if (html != "True")
            {
                html = this.make_list(html);
                html = this.datahtml(html);
                html = this.ifhtml(html);
                html = this.forhtml(html);
                html = this.functionhtml(html);
                return this.writehtml(newurl, html).ToString();
            }
            return "True";
        }

        public string Get_html(string oldurl, string oldstring, string newstring)
        {
            string html = "";
            html = this.readfiles(oldurl);
            html = this.includehtml(html);
            string[] strArray = oldstring.Split(new char[] { '|' });
            string[] strArray2 = newstring.Split(new char[] { '|' });
            for (int i = 0; i < strArray.Length; i++)
            {
                html = html.Replace(strArray[i], strArray2[i]);
            }
            html = this.make_list(html);
            html = this.datahtml(html);
            html = this.ifhtml(html);
            html = this.forhtml(html);
            return this.functionhtml(html);
        }

        public string Get_html(string oldurl, string newurl, string oldstring, string newstring)
        {
            string html = "";
            html = this.readfiles(oldurl);
            html = this.includehtml(html);
            string[] strArray = oldstring.Split(new char[] { '|' });
            string[] strArray2 = newstring.Split(new char[] { '|' });
            for (int i = 0; i < strArray.Length; i++)
            {
                html = html.Replace(strArray[i], strArray2[i]);
            }
            html = this.html_makelist(html, newurl);
            if (html != "True")
            {
                html = this.make_list(html);
                html = this.datahtml(html);
                html = this.ifhtml(html);
                html = this.forhtml(html);
                html = this.functionhtml(html);
                return this.writehtml(newurl, html).ToString();
            }
            return "True";
        }

        public string Get_page(string paths, string page_string, int pageindex, string url)
        {
            string html = "";
            html = this.readfiles(paths);
            html = this.includehtml(html);
            html = this.make_listpage(html, page_string, pageindex, url);
            html = this.make_list(html);
            html = this.datahtml(html);
            html = this.ifhtml(html);
            html = this.forhtml(html);
            return this.functionhtml(html);
        }

        public string Get_page(string paths, string oldstring, string newstring, string page_string, int pageindex, string url)
        {
            string html = "";
            html = this.readfiles(paths);
            html = this.includehtml(html);
            string[] strArray = oldstring.Split(new char[] { '|' });
            string[] strArray2 = newstring.Split(new char[] { '|' });
            for (int i = 0; i < strArray.Length; i++)
            {
                html = html.Replace(strArray[i], strArray2[i]);
            }
            html = this.make_listpage(html, page_string, pageindex, url);
            html = this.make_list(html);
            html = this.datahtml(html);
            html = this.ifhtml(html);
            html = this.forhtml(html);
            return this.functionhtml(html);
        }

        public DataSet GetList(string sql)
        {
            return this.get.GetList(sql);
        }

        public DataSet GetList(string tablename, string strWhere, string filename)
        {
            return this.get.GetList(tablename, strWhere, filename);
        }

        public DataSet GetList(int Top, string tablename, string strWhere, string filedOrder)
        {
            return this.get.GetList(Top, tablename, strWhere, filedOrder);
        }

        public DataSet GetList(string tblName, string fldName, int PageSize, int PageIndex, string orderfldName, int OrderType, string strWhere)
        {
            return this.get.GetList(tblName, fldName, PageSize, PageIndex, orderfldName, OrderType, strWhere);
        }

        public int GetListcount(string sql)
        {
            return this.get.GetListcount(sql);
        }

        public int GetListcount(string tblName, string strWhere)
        {
            return this.get.GetListcount(tblName, strWhere);
        }

        public int GetListcount(string key, string tblName, string strWhere)
        {
            return this.get.GetListcount(key, tblName, strWhere);
        }

        private string html_makelist(string html, string html_url)
        {
            string str = "";
            int startIndex = 0;
            int index = 0;
            int num4 = 0;
            int num5 = 0;
            string str2 = html;
            string newValue = "";
            string tblName = "";
            int pageSize = 0;
            string orderfldName = "";
            string strWhere = "";
            string str8 = ".html";
            string[] strArray = html_url.Split(new char[] { '.' });
            if (strArray.Length > 1)
            {
                str8 = "." + strArray[1];
            }
            try
            {
                string oldValue = this.readtempletepage(html);
                if (oldValue == "1")
                {
                    return str2;
                }
                startIndex = oldValue.IndexOf(":") + 1;
                index = oldValue.IndexOf("}");
                string[] strArray3 = oldValue.Substring(startIndex, index - startIndex).Split(new char[] { '^' });
                if (strArray3.Length != 4)
                {
                    return "Error:002 模板解析错误";
                }
                tblName = strArray3[0].ToString();
                pageSize = System.Convert.ToInt32(strArray3[1]);
                orderfldName = strArray3[2].ToString();
                strWhere = strArray3[3].ToString();
                string fldName = "";
                int orderType = 0;
                int listcount = this.GetListcount(tblName, strWhere);
                if (orderfldName.IndexOf(" asc") > 0)
                {
                    orderType = 0;
                    fldName = orderfldName.Split(new char[] { ',' })[0].Replace(" asc", "");
                }
                else
                {
                    orderType = 0;
                    fldName = orderfldName.Split(new char[] { ',' })[0].Replace(" desc", "");
                    orderType = 1;
                }
                num4 = oldValue.IndexOf("}") + 1;
                num5 = oldValue.IndexOf("{/fenye}");
                int i = 0;
                int num9 = 0;
                if ((listcount % pageSize) > 0)
                {
                    num9 = (listcount / pageSize) + 1;
                }
                else
                {
                    num9 = listcount / pageSize;
                }
                if (num9 == 0)
                {
                    num9 = 1;
                }
                for (int j = 1; j < (num9 + 1); j++)
                {
                    string str11 = str2;
                    DataTable table = this.GetList(tblName, fldName, pageSize, j, orderfldName, orderType, strWhere).Tables[0];
                    foreach (DataRow row in table.Rows)
                    {
                        i++;
                        newValue = newValue + this.replacehtml(row, oldValue.Substring(num4, num5 - num4), tblName, i);
                    }
                    str11 = str11.Replace(oldValue, newValue);
                    string[] strArray4 = html_url.Replace(@"\", "-").Split(new char[] { '-' });
                    string htmlurl = strArray4[strArray4.Length - 1].ToString();
                    if (htmlurl.IndexOf(str8) > -1)
                    {
                        htmlurl = htmlurl.Replace(str8, "");
                    }
                    str11 = str11.Replace("{$page}", this.htmlfoot(listcount, pageSize, j, htmlurl, str8));
                    newValue = "";
                    string str14 = "";
                    str14 = "_" + j;
                    str11 = this.make_list(str11);
                    str11 = this.ifhtml(str11);
                    str11 = this.forhtml(str11);
                    str11 = this.datahtml(str11);
                    str11 = this.functionhtml(str11);
                    Filetemplate.writehtml(html_url.Replace(str8, "") + str14 + str8, str11);
                    str11 = "";
                }
                if (listcount == 0)
                {
                    return "False";
                }
                str = "True";
            }
            catch (Exception exception)
            {
                File.AppendAllText(HttpContext.Current.Server.MapPath(string.Format(@"\log\{0}.txt", DateTime.Now.Ticks.ToString())), "模板标签：fenye 错误 【 " + exception.Message + " 】 文件源：" + HttpContext.Current.Request.Path + "\t\r\n " + str);
                HttpContext.Current.Response.Write("模板标签 fenye 有错误信息，请查看日志</a>");
            }
            return str;
        }

        public string html_url(string FileName)
        {
            return (HttpContext.Current.Server.MapPath("~/") + FileName);
        }

        private string htmlfoot(int rscont, int pagenum, int pageindex, string htmlurl, string p_)
        {
            string str = "";
            string str2 = "";
            string str3 = "";
            int num2 = 0;
            if ((rscont % pagenum) > 0)
            {
                num2 = System.Convert.ToInt32((int) (rscont / pagenum)) + 1;
            }
            else
            {
                num2 = System.Convert.ToInt32((int) (rscont / pagenum));
            }
            if (rscont > 0)
            {
                if (pageindex == 1)
                {
                    str = "<dd class=\"pagefirst\">首页</dd><dd class=\"pageprevious\"><span>上一页</span></li>";
                }
                else
                {
                    str = string.Concat(new object[] { "<li class=\"pagefirst\"><a href='", htmlurl, "_1", p_, "'>首页</a></li><li class=\"pageprevious\"><a href='", htmlurl, "_", pageindex - 1, p_, "'>上一页</a></li>" });
                }
                if (pageindex == num2)
                {
                    str2 = str2 + "<li class=\"pagenext\"><span>下一页</span></li><li class=\"pagelast\"><span>尾页</span></li>";
                }
                else
                {
                    str2 = string.Concat(new object[] { str2, "<li class=\"pagenext\"><a href='", htmlurl, "_", pageindex + 1, p_, "'>下一页</a></li><li class=\"pagelast\"><a href='", htmlurl, "_", num2, p_, "'>尾页</a></li>" });
                }
                str3 = string.Concat(new object[] { "<dt class=\"pageall\">总数:", rscont, " 当前页:", pageindex, "/", num2, "</dt><dd><ul>", str });
            }
            else
            {
                str3 = "<dt class=\"nodata\"><span>没有找到任何数据</span></dt>";
            }
            int num5 = 9;
            string str5 = "";
            int num4 = pageindex / 9;
            for (int i = num4 * 9; i < ((num4 + 1) * num5); i++)
            {
                if (i == 0)
                {
                    i = 1;
                }
                if (i <= num2)
                {
                    if (i == pageindex)
                    {
                        str5 = string.Concat(new object[] { str5, "<li class=\"active\"><span>", i, "</span></li>" });
                    }
                    else
                    {
                        str5 = string.Concat(new object[] { str5, "<li class=\"page\"><a href='", htmlurl, "_", i, p_, "'>", i, "</a></li>" });
                    }
                }
            }
            return (str3 + str5 + str2 + "</dd></ul>");
        }

        public string htmlpage(int rscont, int pagenum, int pageindex, string url, string page_string)
        {
            string str = "";
            string str2 = "";
            string str3 = "";
            int num2 = 0;
            if ((rscont % pagenum) > 0)
            {
                num2 = System.Convert.ToInt32((int) (rscont / pagenum)) + 1;
            }
            else
            {
                num2 = System.Convert.ToInt32((int) (rscont / pagenum));
            }
            if (rscont > 0)
            {
                if (pageindex == 1)
                {
                    str = "<li class=\"pagefirst\"><span>首页</span></li><li class=\"pageprevious\"><span>上一页</span></li>";
                }
                else
                {
                    str = string.Concat(new object[] { "<li class=\"pagefirst\"><a href=", url, page_string, "1>首页</a></li><li class=\"pageprevious\"><a href=", url, page_string, pageindex - 1, ">上一页</a></li>" });
                }
                if (pageindex == num2)
                {
                    str2 = str2 + "<li class=\"pagenext\"><span>下一页</span></li><li class=\"pagelast\"><span>尾页</span></li>";
                }
                else
                {
                    str2 = string.Concat(new object[] { str2, "<li class=\"pagenext\"><a href=", url, page_string, pageindex + 1, ">下一页</a></li><li class=\"pagelast\"><a href=", url, page_string, num2, ">尾页</a></li>" });
                }
                str3 = string.Concat(new object[] { "<dt class=\"pageall\">总数:", rscont, " 当前页:", pageindex, "/", num2, "</dt><dd><ul>", str });
            }
            else
            {
                str3 = "<dt class=\"nodata\"><span>没有找到任何数据</span></dt>";
            }
            int num5 = 9;
            string str5 = "";
            int num4 = pageindex / 9;
            for (int i = num4 * 9; i < ((num4 + 1) * num5); i++)
            {
                if (i == 0)
                {
                    i = 1;
                }
                if (i <= num2)
                {
                    if (i == pageindex)
                    {
                        str5 = string.Concat(new object[] { str5, "<li class=\"active\"><span>", i, "</span></li>" });
                    }
                    else
                    {
                        str5 = string.Concat(new object[] { str5, "<li class=\"page\"><a href=", url, page_string, i, ">", i, "</a></li>" });
                    }
                }
            }
            return (str3 + str5 + str2 + "</dd></ul>");
        }

        private string ifhtml(string html)
        {
            string pattern = Refun.Return_fun("c");
            string newValue = "";
            string str3 = html;
            string str4 = "";
            string str5 = "";
            string sExpression = "";
            int startIndex = 0;
            int num3 = 0;
            int index = 0;
            try
            {
                if (!Regex.IsMatch(html, pattern))
                {
                    return str3;
                }
                foreach (Match match in Regex.Matches(html, pattern))
                {
                    startIndex = match.Value.IndexOf("}") + 1;
                    num3 = match.Value.IndexOf("{fouze}") + 1;
                    index = match.Value.IndexOf("{/ruguo}");
                    sExpression = match.Value.Substring(7, startIndex - 8);
                    str4 = match.Value.Substring(startIndex, (num3 - startIndex) - 1);
                    str5 = match.Value.Substring(num3 + 6, (index - num3) - 6);
                    if (System.Convert.ToBoolean(this.EvalExpress(sExpression)))
                    {
                        newValue = str4;
                    }
                    else
                    {
                        newValue = str5;
                    }
                    str3 = str3.Replace(match.Value, newValue);
                }
            }
            catch (Exception exception)
            {
                File.AppendAllText(HttpContext.Current.Server.MapPath(string.Format(@"\log\{0}.txt", DateTime.Now.Ticks.ToString())), "模板标签：ruguo 错误 【 " + exception.Message + " 】 文件源：" + HttpContext.Current.Request.Path + "\t\r\n " + str4);
                HttpContext.Current.Response.Write("模板标签 ruguo 有错误信息，请查看日志</a>");
            }
            return str3;
        }

        private string includehtml(string html)
        {
            string pattern = Refun.Return_fun("d");
            string url = "";
            string str3 = html;
            try
            {
                if (!Regex.IsMatch(html, pattern))
                {
                    return html;
                }
                foreach (Match match in Regex.Matches(html, pattern))
                {
                    url = match.Value.Replace("{baohan:", "").Replace("{/baohan}", "").Replace("}", "");
                    url = HttpContext.Current.Server.MapPath("~/") + url;
                    str3 = str3.Replace(match.Value, Filetemplate.readfiles(url));
                }
                return str3;
            }
            catch (Exception exception)
            {
                File.AppendAllText(HttpContext.Current.Server.MapPath(string.Format(@"\log\{0}.txt", DateTime.Now.Ticks.ToString())), "模板标签：baohan 错误 【 " + exception.Message + " 】 文件源：" + HttpContext.Current.Request.Path + "\t\r\n " + html);
                HttpContext.Current.Response.Write("模板标签 baohan 有错误信息，请查看日志</a>");
            }
            return "";
        }

        public bool isfile(string files)
        {
            return Filetemplate.isfile(files);
        }

        public bool IsMd5(string str, string md5string)
        {
            return Filetemplate.verifyMd5Hash(str, md5string);
        }

        private string make_list(string html)
        {
            int startIndex = 0;
            int index = 0;
            int num4 = 0;
            int num5 = 0;
            string str = this.make_listloop(html);
            string newValue = "";
            string str4 = "";
            int shownum = 0;
            string orderstring = "";
            string where = "";
            try
            {
                string str8 = this.readtemplete(str);
                if (str8 != "1")
                {
                    string[] strArray = str8.Split(new char[] { '△' });
                    for (int i = 0; i < (strArray.Length - 1); i++)
                    {
                        startIndex = strArray[i].IndexOf(":") + 1;
                        index = strArray[i].IndexOf("}");
                        string[] strArray2 = strArray[i].Substring(startIndex, index - startIndex).Split(new char[] { '^' });
                        str4 = strArray2[0].ToString();
                        shownum = System.Convert.ToInt32(strArray2[1]);
                        orderstring = strArray2[2].ToString();
                        where = strArray2[3].ToString();
                        int num8 = 0;
                        foreach (DataRow row in this.make_url_fun(str4, shownum, orderstring, where).Rows)
                        {
                            num8++;
                            num4 = strArray[i].IndexOf("}") + 1;
                            num5 = strArray[i].IndexOf("{/liebiao}");
                            newValue = newValue + this.replacehtml(row, strArray[i].Substring(num4, num5 - num4), str4, num8);
                        }
                        str = str.Replace(strArray[i], newValue);
                        newValue = "";
                    }
                }
                return str;
            }
            catch (Exception exception)
            {
                File.AppendAllText(HttpContext.Current.Server.MapPath(string.Format(@"\log\{0}.txt", DateTime.Now.Ticks.ToString())), "模板标签：liebiao 错误 【 " + exception.Message + " 】 文件源：" + HttpContext.Current.Request.Path + "\t\r\n " + str);
                HttpContext.Current.Response.Write("模板标签 liebiao 有错误信息，请查看日志</a>");
            }
            return "";
        }

        private string make_listloop(string html)
        {
            int startIndex = 0;
            int index = 0;
            int num4 = 0;
            int num5 = 0;
            string str = html;
            string newValue = "";
            string str4 = "";
            int shownum = 0;
            string orderstring = "";
            string where = "";
            try
            {
                string str8 = this.readtemplateloop(html);
                if (str8 != "1")
                {
                    string[] strArray = str8.Split(new char[] { '★' });
                    for (int i = 0; i < (strArray.Length - 1); i++)
                    {
                        startIndex = strArray[i].IndexOf(":") + 1;
                        index = strArray[i].IndexOf("}");
                        string[] strArray2 = strArray[i].Substring(startIndex, index - startIndex).Split(new char[] { '^' });
                        str4 = strArray2[0].ToString();
                        shownum = System.Convert.ToInt32(strArray2[1]);
                        orderstring = strArray2[2].ToString();
                        where = strArray2[3].ToString();
                        int num8 = 0;
                        foreach (DataRow row in this.make_url_fun(str4, shownum, orderstring, where).Rows)
                        {
                            num8++;
                            num4 = strArray[i].IndexOf("}") + 1;
                            num5 = strArray[i].IndexOf("{/liebiao2}");
                            newValue = newValue + this.replacehtml(row, strArray[i].Substring(num4, num5 - num4), str4, num8, 1);
                        }
                        str = str.Replace(strArray[i], newValue);
                        newValue = "";
                    }
                }
                return str;
            }
            catch (Exception exception)
            {
                File.AppendAllText(HttpContext.Current.Server.MapPath(string.Format(@"\log\{0}.txt", DateTime.Now.Ticks.ToString())), "模板标签：liebiao2 错误 【 " + exception.Message + " 】 文件源：" + HttpContext.Current.Request.Path + "\t\r\n " + str);
                HttpContext.Current.Response.Write("模板标签 liebiao2 有错误信息，请查看日志</a>");
            }
            return "";
        }

        private string make_listpage(string html, string page_string, int pageindex, string url)
        {
            string str = "";
            int startIndex = 0;
            int index = 0;
            int num4 = 0;
            int num5 = 0;
            string str2 = html;
            string newValue = "";
            string tblName = "";
            int pageSize = 0;
            string orderfldName = "";
            string strWhere = "";
            try
            {
                string oldValue = this.readtempletepage(html);
                if (oldValue == "1")
                {
                    return str2;
                }
                startIndex = oldValue.IndexOf(":") + 1;
                index = oldValue.IndexOf("}");
                string[] strArray = oldValue.Substring(startIndex, index - startIndex).Split(new char[] { '^' });
                if (strArray.Length != 4)
                {
                    return "Error:002 模板解析错误";
                }
                tblName = strArray[0].ToString();
                pageSize = System.Convert.ToInt32(strArray[1]);
                orderfldName = strArray[2].ToString();
                strWhere = strArray[3].ToString();
                string fldName = "";
                int orderType = 0;
                int listcount = this.GetListcount(tblName, strWhere);
                if (orderfldName.IndexOf(" asc") > 0)
                {
                    orderType = 0;
                    fldName = orderfldName.Split(new char[] { ',' })[0].Replace(" asc", "");
                }
                else
                {
                    orderType = 0;
                    fldName = orderfldName.Split(new char[] { ',' })[0].Replace(" desc", "");
                    orderType = 1;
                }
                num4 = oldValue.IndexOf("}") + 1;
                num5 = oldValue.IndexOf("{/fenye}");
                int i = 0;
                DataTable table = this.GetList(tblName, fldName, pageSize, pageindex, orderfldName, orderType, strWhere).Tables[0];
                foreach (DataRow row in table.Rows)
                {
                    i++;
                    newValue = newValue + this.replacehtml(row, oldValue.Substring(num4, num5 - num4), tblName, i);
                }
                str2 = str2.Replace(oldValue, newValue).Replace("{$page}", this.htmlpage(listcount, pageSize, pageindex, url, page_string));
                newValue = "";
                str = str2;
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
            return str;
        }

        public DataTable make_url_fun(string html_type, int shownum, string orderstring, string where)
        {
            return this.return_rs(html_type, shownum, orderstring, where);
        }

        public string Md5(string str)
        {
            return Filetemplate.getMd5Hash(str);
        }

        public string readfiles(string url)
        {
            return Filetemplate.readfiles(url);
        }

        private string readtemplateloop(string html_url)
        {
            string pattern = Refun.Return_fun("h");
            string str2 = "";
            if (Regex.IsMatch(html_url, pattern))
            {
                foreach (Match match in Regex.Matches(html_url, pattern))
                {
                    str2 = str2 + match.Value + "★";
                }
                return str2;
            }
            return "1";
        }

        private string readtemplete(string html_url)
        {
            string pattern = Refun.Return_fun("g");
            string str2 = "";
            if (Regex.IsMatch(html_url, pattern))
            {
                foreach (Match match in Regex.Matches(html_url, pattern))
                {
                    str2 = str2 + match.Value + "△";
                }
                return str2;
            }
            return "1";
        }

        private string readtempletepage(string html_url)
        {
            string pattern = Refun.Return_fun("f");
            string str2 = "";
            if (Regex.IsMatch(html_url, pattern))
            {
                foreach (Match match in Regex.Matches(html_url, pattern))
                {
                    str2 = match.Value;
                }
                return str2;
            }
            return "1";
        }

        public string repfilename(string data_name, string file_names, string where)
        {
            return this.return_rs(data_name, file_names, where);
        }

        public string replacehtml(DataRow r, string value, string html_type, int i)
        {
            return Filetemplate.replacehtmls(r, value, html_type, i);
        }

        public string replacehtml(DataRow r, string value, string html_type, int i, int num)
        {
            return Filetemplate.replacehtmls(r, value, html_type, i, num);
        }

        public string return_rs(string data_name, string file_names, string where)
        {
            string str = "";
            DataTable table = new DataTable();
            table = this.GetList(data_name, where, file_names).Tables[0];
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0][file_names].ToString();
            }
            return str;
        }

        public DataTable return_rs(string html_type, int shownum, string orderstring, string where)
        {
            DataTable table = new DataTable();
            return this.GetList(shownum, html_type, where, orderstring).Tables[0];
        }

        public DataSet select(string sql, SqlParameter[] cmdparm)
        {
            return this.get.GetList(sql, cmdparm);
        }

        public bool SendEmail(string to, string from, string fromname, string subject, string body, string userName, string password, string smtpHost, int port)
        {
            return Filetemplate.SendEmail(to, from, fromname, subject, body, userName, password, smtpHost, port);
        }

        private string switchhtml(string html)
        {
            string pattern = Refun.Return_fun("i");
            string newValue = "";
            string str3 = html;
            string str4 = "";
            string str5 = "";
            string sExpression = "";
            int startIndex = 0;
            int num3 = 0;
            int index = 0;
            try
            {
                if (!Regex.IsMatch(html, pattern))
                {
                    return str3;
                }
                foreach (Match match in Regex.Matches(html, pattern))
                {
                    startIndex = match.Value.IndexOf("}") + 1;
                    num3 = match.Value.IndexOf("{fouze}") + 1;
                    index = match.Value.IndexOf("{/ruguo}");
                    sExpression = match.Value.Substring(7, startIndex - 8);
                    str4 = match.Value.Substring(startIndex, (num3 - startIndex) - 1);
                    str5 = match.Value.Substring(num3 + 6, (index - num3) - 6);
                    if (System.Convert.ToBoolean(this.EvalExpress(sExpression)))
                    {
                        newValue = str4;
                    }
                    else
                    {
                        newValue = str5;
                    }
                    str3 = str3.Replace(match.Value, newValue);
                }
            }
            catch (Exception exception)
            {
                File.AppendAllText(HttpContext.Current.Server.MapPath(string.Format(@"\log\{0}.txt", DateTime.Now.Ticks.ToString())), "模板标签：ruguo 错误 【 " + exception.Message + " 】 文件源：" + HttpContext.Current.Request.Path + "\t\r\n " + str4);
                HttpContext.Current.Response.Write("模板标签 ruguo 有错误信息，请查看日志</a>");
            }
            return str3;
        }

        public string Upload(string path, int Filesize, HttpPostedFile UpFile, string docType)
        {
            return Fileuploads.Upload(path, Filesize, UpFile, docType);
        }

        public string Upload(string path, int smallwidth, int filesize, HttpPostedFile UpFile, string docType)
        {
            return Fileuploads.Upload(path, smallwidth, filesize, UpFile, docType);
        }

        public string Upload(string path, string smallpath, int smallwidth, int smallheight, int filesize, HttpPostedFile UpFile, string docType)
        {
            return Fileuploads.Upload(path, smallpath, smallwidth, smallheight, filesize, UpFile, docType);
        }

        public string uploadfiles(string path, int Filesize, HttpPostedFile UpFile, string docType)
        {
            return Fileuploads.UploadFile(path, Filesize, UpFile, docType);
        }

        public bool writehtml(string url, string value)
        {
            return Filetemplate.writehtml(url, value);
        }
    }
}

